Site hosted by Angelfire.com: Build your free website today!

Chapter 5 - Program Looping

List of Questions


Back to Home Page

Question 1

Type in and run the nine programs presented in this chapter. Conpare the output produced by each program with the output presented after each program in the text.

Answer 1

Basic instruction...

Back to Top

Question 2

Write a program to generate and display a table of n and n2, for the integer values of n ranging from 1 to 10. Be certain to print appropriate column headings.


Answer 2


/*Chapter 5 Exercise 2 (5.Ex2)*/

#include <stdio.h>

int main (void)
{
    int n, n2;
    
    printf ("TABLE OF N AND N^2\n\n");
    printf (" N     N^2 \n");
    printf ("---   -----\n");

    n2 = 0;
    
    for (n = 1; n <= 10; ++n) {
        n2 = n*n;
        printf ("%2i     %i\n", n, n2);
        }
    
    return 0;
}    
Back to Top

Question 3

A triangular number can also be generated by the formula:

triangularNumber = n (n + 1) / 2

for any integer value of n. For example, the 10th triangular number number, 55, can be generated by substituting 10 as the value for n in the preceding formula. Write a program that generates a table of triangular numbers using the preceding formula. Have the program generate every fifth triangular number between 5 and 50 (i.e. 5, 10, 15, ..., 50).

Answer 3


/*Chapter 5 Exercise 3 (5.Ex3)*/

#include <stdio.h>

int main (void)
{
    int n = 0, n2 = 0;
    
    printf ("TABLE OF MULTIPLES OF 5\nAND THEIR TRIANGULAR NUMBERS\n\n");
    printf (" M     TN \n");
    printf ("---   ----\n");
   
    do {
        n2 =n * (n + 1) / 2;
        printf ("%2i     %i\n", n, n2);
        n = n + 5;        
        }
    while (n % 5 == 0, n <= 50);

    return 0;
}    
Back to Top

Question 4

The factorial of an integer n, written n!, is the product of the consecutive integers 1 through n. For example, 5 factorial is calculated as:

5! = 5 × 4 × 3 × 2 × 1 = 120

Write a program to generate and print a table of the first 10 factorials.

Answer 4


/*Chapter 5 Exercise 4 (5.Ex4)*/

#include <stdio.h>

int main (void)
{
    int n = 1, n2 = 0, factorial = 1;
    
    printf ("\nTABLE OF FACTORIALS UP TO 10\n\n");
    printf (" N     FN \n");
    printf ("---   ----\n");
    
       do {
        factorial = factorial * (n - n2);
        ++n2; 
        factorial = factorial * n;
        printf ("%2i     %i\n", n, factorial);
        ++n;
        }
        
       while (n2 != 10);
       
    return 0;
}    
Back to Top

Question 5

The following perfectly valid C program was written without much attention paid to it's format. As you will observe, the program is not very readable. Using the program presented in this chapter as examples, reformat the program so that it is more readable. Then type the program into the computer and run it.


#include <stdio.h>
int main(void) {
int n, two_to_the_n;
printf("TABLE OF POWERS OF TWO\n\n");
printf(" n     2 to the n\n");
two_to_the_n=1;
for(n=0;n<=10;++n){
printf("%2i         %i\n",n,two_to_the_n); two_to_the_n*=2;}
return 0;}

Answer 5


#include <stdio.h>

int main(void) 
{

    int n, two_to_the_n;

    printf ("TABLE OF POWERS OF TWO\n\n");
    printf (" n     2 to the n\n");
    
    two_to_the_n = 1;

    for (n = 0; n <= 10; ++n) 
        {
        printf("%2i         %i\n", n, two_to_the_n); 
        two_to_the_n *= 2;
        }
     
    return 0;
}
Back to Top

Question 6

A minus sign placed in front of a field width specification causes the field to be displayed left-justified. Substitute the following printf statement for the corresponding statement in Program 5.2, run the program, and compare the outputs produced by both programs.

            printf ("%-2i         %i\n",n,triangularNumber); 

Answer 6


/*Chapter 5 Exercise 6 (5.Ex6)*/

#include <stdio.h>

int main (void)
{
    int n, n2;
    
    printf ("TABLE OF N AND N^2\n\n");
    printf (" N     N^2 \n");
    printf ("---   -----\n");

    n2 = 0;
    
    for (n = 1; n <= 10; ++n) {
        n2 = n*n;
        printf ("%-2i     %i\n", n, n2);
        }
    
    return 0;
}    

After making the column left-justified, it made all the numbers preceding 10 in the left column to be aligned with the 1 in 10 not the 0.

Back to Top

Question 7

A decimal point before the field width specification in a printf statement has a special purpose. Try to determine its purpose by typing in and running the following program. Experiment by typing in different values each time you are prompted.


#include <stdio.h>

int main (void)
{
    int dollars, cents, count;
    
    for (count = 1; count <= 10; count) {
        printf ("Enter dollars: ");
        scanf ("%i", &dollars);
        printf ("Enter cents: ");
        scanf ("%i", ¢s);
        printf ("$%i.%.2i\n\n", dollars, cents);
    }
    
    return 0;
} 

Answer 7

There are two instances of decimal points in the relevant printf statement. The first acts as a break between the dollars and cents while the second interprets the cents entered. For example, if "12" was entered when prompted for dollars and "3" was entered when prompted for cents the result would be: $12.03.

Back to Top

Question 8

Program 5.5 allows the user to type in only five different numbers. Modify that program so that the user can type in the number of triangular numbers to be calculated.

Answer 8


/*Chapter 5 Exercise 8 (5.Ex8)*/

#include <stdio.h>

int main(void)
{
    int n, number, triangularNumber, counter, counter2;
    
    printf ("Hello,\n       How many triangulars would you like? ");
    scanf  ("%i", &counter2);
    
    for (counter = 1; counter <= counter2; ++counter) {
    printf ("\nWhat triangular would you like? ");
    scanf  ("%i", &number);
    
    triangularNumber = 0;
    
    for (n = 1; n <= number; ++n)
         triangularNumber += n;
         
    printf ("\nThe triangular number of %i is %i\n", number, triangularNumber);
    }
    
    return 0;
}
Back to Top

Question 9

Rewrite programs 5.2 through 5.5, replacing all uses of the for statement with equivalent while statements. Run each program to verify that both versions are identical.

Answer 9


/*Chapter 5 Exercise 9a (5.Ex9a)*/

#include <stdio.h>
int main (void)
{
    int triangularNumber = 0;
    int n = 1;
   
    while (n <= 200) {
          triangularNumber = triangularNumber + n;
          n = n + 1;
          }
    
    printf ("The 200th triangular number is %i", triangularNumber);
           
    return 0;
}   


/*Chapter 5 Exercise 9b (5.Ex9b)*/

#include <stdio.h>

int main (void)
{
    int n = 1;
    int triangularNumber = 0;
    
    printf ("\nTABLE OF TRIANGULAR NUMBERS\n");
    printf (" n    Sum from 1 to n\n");
    printf ("---   ---------------\n");
    
    while (n <= 10) {
          triangularNumber += n;
          printf ("%2i           %i\n", n, triangularNumber);
          ++n;
          }
          
    return 0;
}


/*Chapter 5 Exercise 9c (5.Ex9c)*/

#include <stdio.h>
int main (void)
{
    int n = 1;
    int triangularNumber = 0;
    int number;
    
    printf ("\nWhat triangular number would you like? ");
    scanf  ("%i", &number);
    
    while (n <= number) {
          
          triangularNumber += n;
          ++n;
          }
          
    printf ("\nThe triangular number of %i is %i\n", number, triangularNumber);
    
    return 0;
} 


/*Chapter 5 Exercise 9d (5.Ex9d)*/

#include <stdio.h>
int main (void)
{
   
   int number;
   int n = 1;
   int counter = 1;
   int triangularNumber = 0;
   
   while (counter <= 5) {
         printf ("\nWhat triangular number would you like? ");
         scanf  ("%i", &number);
         
         while (n <= number) {
               triangularNumber += n;
               ++n;
               }
         ++counter;
   printf ("\nTriangular number %i is %i\n", number, triangularNumber);            
   }
   
   return 0;      
}
Back to Top

Question 10

What would happen if you typed a negative number into Program 5.8? Try it and see.

Answer 10

The program reverses the number but it also adds a minus sign to every digit of that number, making it a line of single digit, negative numbers.

Back to Top

Question 11

Write a program that calculates the sum of the digits of an integer. For example, the sum of the digits of the number 2155 is 2 + 1 + 5 + 5 or 13. The program should accept any arbitrary number typed in by the user.

Answer 11


/*Chapter 5 Exercise 11 (5.Ex11)*/

#include <stdio.h>
int main (void)
{
    int number, right_digit;
    int sum = 0;
    
    	printf ("Enter your number (Keep in mind the max length of a long int). ");
    	scanf  ("%i", &number);
    
       do {
          right_digit = number % 10;
          number = number / 10;  
          sum += right_digit;     
       }   
       while (number > 0);        
        
       printf ("The sum of the digits of your number is %i", sum);
}   
Back to Top

Back to Home Page